﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using BicycleTrafficSimulator.DataModel;

namespace BicycleTrafficSimulator.Presentation
{
    class InfoPage : DrawableGameComponent
    {
        private Scene scene;

        private Texture2D backgroundTexture;

        private Vector2 shareButtonPosition;
        private Vector2 exitButtonPosition;

        private Rectangle shareButtonRect;

        public Rectangle ShareButtonRect
        {
            get { return shareButtonRect; }
            set { shareButtonRect = value; }
        }
        private Rectangle exitButtonRect;

        public Rectangle ExitButtonRect
        {
            get { return exitButtonRect; }
            set { exitButtonRect = value; }
        }

        MouseState mouseState;
        MouseState previousMouseState;

        private SpriteFont spriteFont;
        private SpriteFont spriteFontUser;
        private User user;
        private bool postOnFb = false;

        public bool PostOnFb
        {
            get { return postOnFb; }
            set { postOnFb = value; }
        }

        private bool logout = false;

        public bool Logout
        {
            get { return logout; }
            set { logout = value; }
        }


        public InfoPage(Scene _scene, User user)
            : base(_scene)
        {
            scene = _scene;
            this.user = user;
        }

        public override void Initialize()
        {
            base.Initialize();
            //set the position of the buttons
            shareButtonPosition = new Vector2(180, 650);
            exitButtonPosition = new Vector2(700, 640);

            shareButtonRect = new Rectangle((int)shareButtonPosition.X, (int)shareButtonPosition.Y, 250, 70);
            exitButtonRect = new Rectangle((int)exitButtonPosition.X, (int)exitButtonPosition.Y, 250, 70);

            //get the mouse state
            mouseState = Mouse.GetState();
            previousMouseState = mouseState;

            backgroundTexture = scene.Content.Load<Texture2D>("infopage");
            spriteFont = scene.Content.Load<SpriteFont>("ScreenFont");
            spriteFontUser = scene.Content.Load<SpriteFont>("ScreenFontUser");
        }

        public override void Update(GameTime gameTime)
        {

            //wait for mouseclick
            mouseState = Mouse.GetState();
            if (previousMouseState.LeftButton == ButtonState.Pressed &&
               mouseState.LeftButton == ButtonState.Released)
            {
                MouseClicked(mouseState.X, mouseState.Y);
            }

            previousMouseState = mouseState;
            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            scene.SpriteBatchMenu.Begin();
            scene.SpriteBatchMenu.Draw(backgroundTexture, Vector2.Zero, Color.White);
            DrawText();
            scene.SpriteBatchMenu.End();
            base.Draw(gameTime);

        }

        public void DrawText()
        {
            scene.SpriteBatchMenu.DrawString(spriteFontUser, " " + user.Name, new Vector2(300, 290), Color.White);
            scene.SpriteBatchMenu.DrawString(spriteFontUser, " " + user.Age, new Vector2(300, 325), Color.White);
            scene.SpriteBatchMenu.DrawString(spriteFont, " " +  user.Score.Value, new Vector2(400, 470), Color.White);
        }

        void MouseClicked(int x, int y)
        {
            //creates a rectangle of 10x10 around the place where the mouse was clicked
            Rectangle mouseClickRect = new Rectangle(x, y, 10, 10);

            if (mouseClickRect.Intersects(ShareButtonRect)) 
            {
                postOnFb = true;
            }
            else if (mouseClickRect.Intersects(ExitButtonRect)) 
            {
                scene.GameStateScene = Scene.GameState.StartMenu;
                logout = true;
                //scene.Exit();
            }
        }
    }
}
